package com.whnk.pm.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.whnk.common.pm.constants.EmpConstants;
import com.whnk.common.pm.enums.PmDateType;
import com.whnk.common.support.Convert;
import com.whnk.pm.domain.DcInfo;
import com.whnk.pm.domain.WdInfo;
import com.whnk.pm.mapper.DcInfoMapper;
import com.whnk.pm.mapper.WdInfoMapper;
import com.whnk.pm.service.IWdInfoService;

/**
 * 工作日历 服务层实现
 * 
 * @author whwhnk
 * @date 2019-07-11
 */
@Service
public class WdInfoServiceImpl implements IWdInfoService 
{
	@Autowired
	private WdInfoMapper wdInfoMapper;
	
	@Autowired
	private DcInfoMapper dcInfoMapper;

	/**
     * 查询工作日历信息
     * 
     * @param wdId 工作日历ID
     * @return 工作日历信息
     */
    @Override
	public WdInfo selectWdInfoById(Integer wdId)
	{
	    return wdInfoMapper.selectWdInfoById(wdId);
	}
	
	/**
     * 查询工作日历列表
     * 
     * @param wdInfo 工作日历信息
     * @return 工作日历集合
     */
	@Override
	public List<WdInfo> selectWdInfoList(WdInfo wdInfo)
	{
	    return wdInfoMapper.selectWdInfoList(wdInfo);
	}
	
    /**
     * 新增工作日历
     * 
     * @param wdInfo 工作日历信息
     * @return 结果
     */
	@Override
	public int insertWdInfo(WdInfo wdInfo)
	{
	    return wdInfoMapper.insertWdInfo(wdInfo);
	}
	
	/**
     * 修改工作日历
     * 
     * @param wdInfo 工作日历信息
     * @return 结果
     */
	@Override
	public int updateWdInfo(WdInfo wdInfo)
	{
	    return wdInfoMapper.updateWdInfo(wdInfo);
	}

	/**
     * 删除工作日历对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteWdInfoByIds(String ids)
	{
		return wdInfoMapper.deleteWdInfoByIds(Convert.toStrArray(ids));
	}

	/**
	 * 根据输入的年份，生成日历数据
	 * 
	 * @param wdInfo
	 * @return
	 * @throws Exception 
	 */
	@Override
	public int createWorkDt(Integer wdYear){
		if(null == wdYear) 
			wdYear = 2019;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		int rows = 0;
		try {
			java.util.Date start = sdf.parse(wdYear+"0101");//开始时间
	        java.util.Date end = sdf.parse(wdYear+"1231");//结束时间
	        List<Date> lists = dateSplit(start, end);
	        
	        // 根据年份，删除日历表的数据
	        wdInfoMapper.deleteWdInfoByYear(wdYear);
	        
	        // 循环写入日历数据
	        if (!lists.isEmpty()) {
	            for (Date date : lists) {
	            	WdInfo wdi = new WdInfo();
	                Calendar cal = Calendar.getInstance();
	                cal.setTime(date);
	
	                int year = cal.get(Calendar.YEAR);
	                int month = cal.get(Calendar.MONTH) + 1;
	                int day = cal.get(Calendar.DATE);
	                int week = cal.get(Calendar.DAY_OF_WEEK)-1;
	
	                wdi.setWdYear(year); // 年
	                wdi.setWdMonth(month);// 月
	                wdi.setWdDay(day); // 日
	                wdi.setWdWeek(EmpConstants.WEEK[week]); // 周几
	                wdi.setWdDate(date); // 日期
	                wdi.setWdDtType(checkDateType(date,wdYear)); // 日期类型
	                wdi.setWdNote("通过手工调用服务生成【"+new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date())+"】"); // 备注
	                wdInfoMapper.insertWdInfo(wdi);
	                rows++;
	            }
	        }
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return rows;
	}
	
	
	private static List<Date> dateSplit(Date start, Date end){
        Long spi = end.getTime() - start.getTime();
        Long step = spi / (24 * 60 * 60 * 1000);// 相隔天数
        List<Date> dateList = new ArrayList<Date>();
        dateList.add(end);
        for (int i = 1; i <= step; i++) {
            dateList.add(new Date(dateList.get(i - 1).getTime() - (24 * 60 * 60 * 1000)));// 比上一天减一
        }
        return dateList;
    }
	
	 /**
     * 判断是否为节假日，0-工作日，1-周末，2-节日，3-假日，4-补班
     * @param date
     * @return
     */
    private String checkDateType(Date date,Integer year){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        
        List<String> festivals = getDaysByDtType(year,PmDateType.FESTIVALS.getCode()); // 节日
        List<String> holidays = getDaysByDtType(year,PmDateType.HOLIDAYS.getCode()); // 假日
        List<String> workdays = getDaysByDtType(year,PmDateType.MAKEUP_DAYS.getCode()); // 补班
        
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        
        String result = PmDateType.WORK_DAYS.getCode(); // 工作日
        
        // 周末
        if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY)
        	result = PmDateType.WEEKEND.getCode();
        
        // 节日
        if(festivals.contains(sdf.format(date)))
        	result = PmDateType.FESTIVALS.getCode();
        
        // 假日
        if(holidays.contains(sdf.format(date))) 
        	result = PmDateType.HOLIDAYS.getCode();
        
        // 补班
        if(workdays.contains(sdf.format(date))) 
        	result = PmDateType.MAKEUP_DAYS.getCode();
        
        return result;
        
    }
    
    /**
     * 日期信息
     * 
     * @return
     */
    private List<String> getDaysByDtType(Integer year,String wdDtType){
    	DcInfo dcInfo = new DcInfo();
        dcInfo.setDcYear(String.valueOf(year));
        dcInfo.setWdDtType(wdDtType);
    	List<String> days = dcInfoMapper.selectDcDateByYear(dcInfo);
    	if(null != days)
    		days = new ArrayList<String>();
    	return days;
    }
   
}
